//
//  DetailsViewController.m
//  futureLive
//
//  Created by zhoubiwen on 15/11/17.
//  Copyright © 2015年 ZHOU_LI_SUN. All rights reserved.
//

#import "DetailsViewController.h"
#import "DetailsCollectionViewCell.h"
#import "NetWorkManager.h"
#import "DetailsModel.h"

@interface DetailsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
//存储数据源的数组
@property (nonatomic, strong) NSMutableArray *sourceArray;
@property (nonatomic, strong) UICollectionView *collectView;

@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建布局类
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置itemSize
    flowLayout.itemSize                    = CGSizeMake(self.view.bounds.size.width - self.view.bounds.size.width / 2 - 8, 250);
    //最小的行间距
    flowLayout.minimumLineSpacing          = 10;
    //item的间距
    flowLayout.minimumInteritemSpacing     = 5;
    //分区头
    flowLayout.headerReferenceSize         = CGSizeMake(100, 120);

    //创建collectionView
    UICollectionView *collectionView       = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flowLayout];
    //设置数据源和代理
    collectionView.dataSource              = self;
    collectionView.delegate                = self;

    collectionView.backgroundColor         = [UIColor colorWithRed:200/256.0 green:200/256.0 blue:120/256.0 alpha:1];
   
    self.collectView                       = collectionView;
    
   
    [self.view addSubview:collectionView];
    [self registHeaderAndCell];
    [self initData];
}

- (void)registHeaderAndCell {

    //注册cell到重用池
    [self.collectView registerClass:[DetailsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //注册collectionView分区头
    [self.collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

}
- (void)initData {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",homeDetailsURL,self.value];
    [NetWorkManager getJsonDataWithURL:urlString success:^(id data) {
        self.sourceArray = [NSMutableArray new];
        NSMutableArray *dataArr = data[@"data"];
        for (NSDictionary *itemDict in dataArr) {
            NSMutableArray *itemArr = itemDict[@"itemList"];
            for (NSDictionary *itemListDict in itemArr) {
                DetailsModel *model = [[DetailsModel alloc] init];
                [model setValuesForKeysWithDictionary:itemListDict];
                [self.sourceArray addObject:model];
            }
        }
        
        [self.collectView reloadData];
        
    } fail:^{
        NSLog(@"网络数据请求失败.");
    }];
}

#pragma mark------collectionView
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.sourceArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    DetailsModel *model = self.sourceArray[indexPath.item];
    cell.detailsModel = model;
    
    return cell;
    
}

//分区头
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    headerView.backgroundColor = [UIColor blueColor];
    
    return headerView;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
